package com.qlk.odcinterface.rest;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.qlk.common.utils.wrap.WrapMapper;
import com.qlk.common.utils.wrap.Wrapper;
import com.qlk.odcinterface.domain.PmsCouponChannel;
import com.qlk.odcinterface.sdk.api.request.PmsCouponChannelRequest;
import com.qlk.odcinterface.sdk.api.response.PmsCouponChannelResponse;
import com.qlk.odcinterface.service.PmsCouponChannelService;

/**
 * 满减促销渠道对应表REST服务：提供有关满减促销渠道对应表的接口
 * 
 * @author zhaoyanan
 * @version 2016-4-14 14:11:37
 * 
 */
@Component
@Path("services")
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
public class PmsCouponChannelResource {

    private final Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    private PmsCouponChannelService pmsCouponChannelService; 

    /**
     * 查询满减促销渠道对应表信息
     * 
     * @param request
     *            满减促销渠道对应表请求参数
     * @return 满减促销渠道对应表返回对象
     * 
     */
    @POST
    @Path("/pmsCouponChannel/getPmsCouponChannel")
    public Wrapper<?> getPmsCouponChannel(PmsCouponChannelRequest request) {
        if (null == request || null == request.getId()) {
            this.logger.error("getPmsCouponChannel 传入参数有误");
            return WrapMapper.illegalArgument();
        }

        try {
            PmsCouponChannel pmsCouponChannel = pmsCouponChannelService.getPmsCouponChannelById(request.getId());
            PmsCouponChannelResponse response = convert(pmsCouponChannel);
            return WrapMapper.ok().result(response);
        } catch (Exception e) {
            this.logger.error("查询满减促销渠道对应表数据异常", e);
            return WrapMapper.error();
        }
    } 

    // 数据转换
    private PmsCouponChannelResponse convert(PmsCouponChannel pmsCouponChannel) {
        if (null == pmsCouponChannel) {
            return null;
        }

        PmsCouponChannelResponse pmsCouponChannelResponse = new PmsCouponChannelResponse();
        BeanUtils.copyProperties(pmsCouponChannel, pmsCouponChannelResponse);
        return pmsCouponChannelResponse;
    }

    // 数据转换
    private List<PmsCouponChannelResponse> convertList(List<PmsCouponChannel> pmsCouponChannels) {
        if (CollectionUtils.isEmpty(pmsCouponChannels)) {
            return null;
        }

        List<PmsCouponChannelResponse> list = new ArrayList<PmsCouponChannelResponse>(pmsCouponChannels.size());
        for (PmsCouponChannel pmsCouponChannel : pmsCouponChannels) {
            list.add(convert(pmsCouponChannel));
        }
        return list;
    } 

}
